/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.nnaplanner.DAL;

import java.util.List;
import javax.jdo.annotations.*;

/**
 *
 * @author david
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DALStop {

    @PrimaryKey
    @Persistent
    private String _id;
    @Persistent
    private String _name;
    @Persistent
    private String _description;
    @Persistent
    private float _longitude;
    @Persistent
    private float _latitude;
    @Persistent
    private boolean _connected;
    @Persistent
    private int _connections;

    @Persistent(defaultFetchGroup = "true")
    private List<String> _keywords;


    /**
     * Deafult Constructor
     * @param id - The unique ID to identify this stop. Using ATCOCode as this identifier
     * @param name - The name of the station/stop
     * @param description - A description of the stop to help Identify it
     */
    public DALStop(String id, String name, String description) {
        _id = id;
        _name = name;
        _connected = false;
        _description = description;
    }

    /**
     * Get a stop based on a given ID
     * @param ID the Id of the stop you want to get
     * @return The Stop basd onthe given ID
     */
    public DALStop getByID(String ID) {
        DALStop ds = new DALStop();
        ds = DAL.getByID(ds, ID);
        DAL.get().getPersistenceManager().close();
        return ds;
    }

    /**
     * Creates a DALStop with nothing set
     */
    public DALStop() {
        _id = "";
        _name = "";
        _description = "";
        _connected = false;
        _connections = 0;
    }

    /**
     * Get this stop ID
     * @return the stop ID
     */
    public String getID() {
        return _id;
    }

    /**
     * Set the stop ID
     * @param ID The ID string for this stop
     */
    public void setID(String ID) {
        _id = ID;
    }

    /**
     * Get the name of this stop
     * @return The stop Name
     */
    public String getName() {
        return _name;
    }

    /**
     * Set the name of the stop
     * @param val
     */
    public void setName(String val) {
        _name = val;
    }

    /**
     * Get the description for this stop
     * @return The description
     */
    public String getDescription() {
        return _description;
    }

    /**
     * Set the description for this stop
     * @param val The stop description
     */
    public void setDescription(String val) {
        this._description = val;
    }

    /**
     * The Latitude of this stop
     * @return he latitude of this stop
     */
    public float getLatitude() {
        return _latitude;
    }


    /**
     * Set the latitude
     * @param latitude
     */
    public void setLatitude(float latitude) {
        _latitude = latitude;
    }

    /**
     * Set the Longitude
     * @param longitude
     */
    public void setLongitude(float longitude) {
        _longitude = longitude;
    }

    /**
     * GEt the longitude
     * @return The longitude for this stop
     */
    public float getLongitude() {
        return _longitude;
    }

    /**
     * Is this stop connected to any other stop?
     * @return True if it has a connection
     */
    public boolean isConnected() {
        return _connected;
    }

    /**
     * Set this to true when you add this to a connection
     * @param connected
     */
    public void setConnected(boolean connected) {
        _connected = connected;
    }

    /**
     * Save this stop
     * @return The id of the saved stop
      */
    public String save() {
        return save(this);
    }

    /**
     * Delete Stop
     * @return True if deleted
     */
    public boolean delete() {
        return delete(this);
    }

    /**
     * A List of key words for this stop
     * @return The keywords for this stop
     */
    public List<String> getKeywords() {
        return _keywords;
    }

    /**
     * Set the keywords for this stop. These key words are used to search for
     * stops in the database, as GAE does not support full text search
     * @param _keywords
     */
    public void setKeywords(List<String> _keywords) {
        this._keywords = _keywords;
    }

    /**
     * Save the Stop
     * @param ds The stop you want to save
     * @return The id of the
     */
    public String save(DALStop ds) {
        DAL.save(ds);
        return ds.getID();
    }

    /**
     * Is this object equal to another object
     * @param o The object you want to check for equality
     * @return True if equal
     */
    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (!(o instanceof DALStop)) {
            return false;
        }
        DALStop d = (DALStop) o;
        return this._id.equals(d.getID());
    }

    /**
     * Returns the number of connections for this stop
     * @return
     */
    public int getConnectionCount() {
        return _connections;
    }

    /**
     * Sets the number of connections for this stop
     * @param count The number of connections
     */
    public void setConnectionCount(int count) {
        this._connections = count;
    }

  
    /**
     * The string representation for this object. Basicall its ID
     * @return
     */
    @Override
    public String toString() {
        return  getClass().getName() + this._id;
    }

    /**
     * The hash code for this class. Used in collections with has maps
     * @return the hash code for this object
     */
    @Override
    public int hashCode() {
        return this._id.hashCode();
    }

    /**
     * Deletes the stop that is provided as the parameter
     * @param stop The stop to delete
     * @return True if deleted
     */
    public boolean delete(DALStop stop) {
        DAL.delete(stop, stop.getID());
        return true;
    }
}
